//
//  UIViewController+Category.m
//  dudu_oc_master
//
//  Created by on 2019/11/26.
//  Copyright © 2019 Shawn Ma. All rights reserved.
//

#import "UIViewController+Category.h"
#import "TJBaseNavController.h"
#import "SPActivityIndicatorView.h"

@implementation UIViewController (Category)


+ (void)load {
    ExchangeImplementations(UIViewController.class, @selector(presentViewController:animated:completion:), @selector(tj_presentViewController:animated:completion:));
}

- (void)tj_presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion {
    if (@available(iOS 13.0, *)) {
        if (viewControllerToPresent.modalPresentationStyle == UIModalPresentationAutomatic || viewControllerToPresent.modalPresentationStyle == UIModalPresentationPageSheet) {
            viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
        }
    }
    [self tj_presentViewController:viewControllerToPresent animated:flag completion:completion];
}

- (void)toLastViewController {
    if (self.navigationController) {
        if (self.navigationController.viewControllers.count == 1) {
            if (self.presentingViewController) {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        } else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    } else if(self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}


- (BOOL)isContainNavChildViewController:(NSString *)controller {
    
    if (![Tools currentNavController].viewControllers.count) return NO;
    __block BOOL isContains = NO;
    [self.navigationController.viewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([NSStringFromClass(obj.class) isEqualToString:controller]) {
            isContains = YES;
            *stop = YES;
        }
    }];
    
    return isContains;
}

- (void)dismissToRootViewController {
    UIViewController *vc = self;
    while (vc.presentingViewController) {
        vc = vc.presentingViewController;
    }
    [vc dismissViewControllerAnimated:YES completion:nil];
}


- (void)dismissToRootViewControllerWithAnimated:(BOOL)animated {
    UIViewController *vc = self;
    while (vc.presentingViewController) {
        vc = vc.presentingViewController;
    }
    [vc dismissViewControllerAnimated:animated completion:nil];
}


- (UIViewController *)presentViewControllerWithName:(NSString *)controllerName title:(nullable NSString *)title animated:(BOOL)animated {
    Class clsss = NSClassFromString(controllerName);
    if (!clsss) return nil;
    UIViewController *controller = [[clsss alloc] init];
    if (title) {
        controller.title = title;
    }
    TJBaseNavController *navVC = [[TJBaseNavController alloc] initWithRootViewController:controller];
    [self presentViewController:navVC animated:animated completion:nil];
    return controller;
}


- (NSArray *)removeNavChildViewControllers:(NSArray *)controllers {
    
    if (!self.navigationController.viewControllers.count) return nil;
    if (!controllers.count) return self.navigationController.viewControllers;
    
    NSMutableArray *array = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    [controllers enumerateObjectsUsingBlock:^(NSString *targetController, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.navigationController.viewControllers enumerateObjectsUsingBlock:^(UIViewController *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([NSStringFromClass([obj class]) isEqualToString:targetController]) {
                [array removeObject:obj];
            }
        }];
    }];
    return array.copy;
}

- (void)alertWithTitle:(nullable NSString *)title message:(nullable NSString *)message leftButtonName:(nullable NSString *)leftButtonName rightButtonName:(nullable NSString *)rightButtonName leftButtonBlock:(nullable void (^)(void))leftButtonBlock rightButtonBlock:(nullable void (^)(void))rightButtonBlock {
    
    if (![message isKindOfClass:NSString.class]) message = nil;
    if (![title isKindOfClass:NSString.class]) title = nil;
    
    if (!message.replacingEmptyString.length && !title.replacingEmptyString.length) return;
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    if (leftButtonName) {
        [alert addAction:[UIAlertAction actionWithTitle:leftButtonName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (leftButtonBlock) leftButtonBlock();
        }]];
    }
    
    if (rightButtonName) {
        [alert addAction:[UIAlertAction actionWithTitle:rightButtonName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (rightButtonBlock) rightButtonBlock();
        }]];
    }
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)showloading {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.8];
    hud.mode = MBProgressHUDModeCustomView;
    hud.removeFromSuperViewOnHide = YES;
    CGPoint offset = hud.offset;
    offset.y = 0;
    hud.offset = offset;
    hud.margin = 20;
    hud.graceTime = 1;
    SPActivityIndicatorView *loadingView = [[SPActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    hud.customView = loadingView;
    [loadingView startAnimating];
    loadingView.tintColor = UIColor.whiteColor;

 }

- (void)hiddenloading {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}


@end


@implementation UINavigationController (category)

- (UIViewController *)pushViewControllerWithName:(NSString *)controllerName title:(nullable NSString *)title animated:(BOOL)animated {
    Class clsss = NSClassFromString(controllerName);
    UIViewController *controller = [[clsss alloc] init];
    if (title) controller.title = title;
    controller.hidesBottomBarWhenPushed = YES;
    [self pushViewController:controller animated:animated];
    return controller;
}


@end

